package model;

import entity.Class;
import entity.ClassRoomBlock;
import entity.TeacherClassDetails;
import iface.IModel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Noi
 */
public class ClassTableModel extends AbstractTableModel implements IModel<Class> {

    private manager.Manager manager;

    public ClassTableModel(manager.Manager manager) {
        this.manager = manager;
    }

    @Override
    public int getRowCount() {
        int count = 0;
        for (int i = 0; i < this.manager.getClassManager().size(); i++) {
            count++;
        }
        return count;
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Class obj = this.manager.getClassManager().get(rowIndex);
        ClassRoomBlock objRoomBlock = this.manager.getClassRoomBlockManager().getById(obj.getClassRoomBlockID());
        TeacherClassDetails teacherClass = this.manager.getTeacherClassDetailsManager().getByClassID(obj.getClassID());
        switch (columnIndex) {
            case 0:
                return objRoomBlock;
            case 1:
                return obj.getClassName();
            case 2:
                return obj.getFromYear();
            case 3:
                return obj.getToYear();
            case 4:
                return this.manager.getTeacherManager().getById(teacherClass.getTeacherID()).getTeacherName();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Khối Lớp";
            case 1:
                return "Tên Lớp";
            case 2:
                return "Từ Năm Học";
            case 3:
                return "Đến Năm Học";
            case 4:
                return "Giáo Viên";
            default:
                return null;
        }
    }

    @Override
    public void add(Class obj) {
        this.manager.getClassManager().add(obj);
        this.fireTableDataChanged();
    }

    @Override
    public void replace(int index, Class obj) {
        this.manager.getClassManager().replace(index, obj);
        this.fireTableDataChanged();
    }

    @Override
    public void refresh() {
        this.fireTableDataChanged();
    }

    @Override
    public void remove(Class obj) {
        this.manager.getClassManager().remove(obj);
        this.fireTableDataChanged();
    }

    @Override
    public Class get(int i) {
        return this.manager.getClassManager().get(i);
    }
}